package com.dfrz.stockMarket.service;

import java.util.List;

import com.dfrz.stockMarket.bean.MessageBean;
import com.dfrz.stockMarket.bean.UserBean;
import com.dfrz.stockMarket.cons.Constants;
import com.dfrz.stockMarket.dao.UserDao;
import com.dfrz.stockMarket.dao.UserInfoDao;
import com.dfrz.stockMarket.dao.UserOrderDao;
import com.dfrz.stockMarket.dao.UserPurseDao;
import com.dfrz.stockMarket.dao.UserStocksDao;
import com.dfrz.stockMarket.model.UserInfoModel;
import com.dfrz.stockMarket.model.UserOrderModel;
import com.dfrz.stockMarket.model.UserPurseModel;
import com.dfrz.stockMarket.model.UserStocksModel;

/**
 * 调用相关Dao类进行数据持久化操作
 * @author Niko
 *
 */
public class UserService {
	public static MessageBean getUserBeanByName(String name) throws Exception {
		UserInfoDao userInfoDao = new UserInfoDao();
		UserInfoModel userInfoModel = (UserInfoModel) userInfoDao.getUserInfoByName(name).getMsg();
		
		int userID = userInfoModel.getUserID();
		
		UserOrderDao userOrderDao = new UserOrderDao();
		List<UserOrderModel> userOrderModels = (List<UserOrderModel>) userOrderDao.getUserOrderByID(userID).getMsg();
		
		UserPurseDao userPurseDao = new UserPurseDao();
		UserPurseModel userPurseModel = (UserPurseModel) userPurseDao.getUserPurseByID(userID).getMsg();
		
		UserStocksDao userStocksDao = new UserStocksDao();
		List<UserStocksModel> userStocks = (List<UserStocksModel>)userStocksDao.getUserStocksByID(userID).getMsg();
		
		UserBean userBean = new UserBean(userInfoModel,userOrderModels, userPurseModel,userStocks);
		return new MessageBean(true, userBean);
	}
	
	public MessageBean saveUser(UserBean userBean) throws Exception{
		
		UserInfoModel userInfoModel = userBean.getUserInfoModel();
		List<UserOrderModel> userOrderModels = userBean.getUserOrderModels();
		UserPurseModel userPurseModel = userBean.getUserPurseModel();
		List<UserStocksModel> userStocksModels = userBean.getUserStocksModels();
		UserDao userDao = new UserDao();
		userDao.save(userInfoModel);
		userDao.save(userPurseModel);
		for (UserOrderModel userOrderModel : userOrderModels) {
			userDao.save(userOrderModel);
		}
		for (UserStocksModel userStocksModel : userStocksModels) {
			userDao.save(userStocksModel);
		}
		return new MessageBean(true,Constants.USER_SUCCESS_TYPE3);
	}
	public static MessageBean saveUserInfo(UserInfoModel userinfo) throws Exception{
		return UserDao.save(userinfo);
	}
	public static void saveUserStocksModel(UserStocksModel userStocksModel) throws Exception{
		
			UserDao.save(userStocksModel);
	}
	public static void saveUserOrderModel(UserOrderModel userOrderModel) throws Exception{
		
		UserDao.save(userOrderModel);
}
	/*public static void saveUserOrderModel(UserOrderModel userOrderModel) throws Exception{
		
		UserDao.save(userOrderModel);
}*/
}
